﻿
using System.Collections.Generic;
using agsXMPP;
namespace sharpxbot.Core.Security
{
    public class UsageCountersDictionary
    {
        public UsageCountersDictionary() : this(3,3000) { }

        public UsageCountersDictionary(int counterLimit, int timeLimit)
        {
            this.timeLimit = timeLimit;
            this.counterLimit = counterLimit;

            countersDictionary = new Dictionary<Jid, IUsageCounter>();
        }

        public void PerformCounter(Jid realJid)
        {
            IUsageCounter counter = null;
            if (countersDictionary.TryGetValue(realJid, out counter))
            {
                counter.Perform();
            }
            else
            {
                IUsageCounter newCounter = CreateUsageCounter();
                newCounter.Perform();
                countersDictionary[realJid] = newCounter;
            }
        }

        public bool ExceedsLimit(Jid realJid)
        {
            IUsageCounter counter = null;
            if (countersDictionary.TryGetValue(realJid, out counter))
            {
                return counter.ExceedesLimit;
            }
            else
            {
                return false;
            }
        }

        private IUsageCounter CreateUsageCounter()
        {
            return new UsageCounter(counterLimit,timeLimit);
        }

        private Dictionary<Jid, IUsageCounter> countersDictionary;
        private int timeLimit;
        private int counterLimit;
    }
}
